package main

import (
	"os"
	"os/signal"
	"server/src/logging"
	"server/src/protocol808"
	"server/src/storage"
	"syscall"
)

const VER string = "接收器服务器 v1.3 build 220313"

func main() {
	//var body string
	//var err error
	//var n int
	protocol := protocol808.Request{}
	err, header, body := protocol.Parse("7E01020005060623450335002617373352A3927E")
	_ = header
	_ = body
	//var header *protocol808.Header
	//var body string
	//var err error
	//var n int
	//var objectId bson.ObjectId
	//var cmdOid bson.ObjectId
	//var responseStr string
	//var sendStr string
	initGlobal()

	storage.Init(storage.RootPath())

	// 启动日志
	logging.DisableBootScreenLog()
	logging.LogBoot()

	zLog(nil, VER, " 启动")

	cfg, err := loadConfig("")
	if err != nil {
		zLog(nil, err.Error())
		return
	}

	dbo, err := NewDBOperation(cfg)
	if err != nil {
		zLog(nil, "数据库连接失败，程序退出:", err)
		return
	} else {
		zLog(nil, "数据库连接成功")
	}
	defer dbo.Close()

	connManager := NewConnManager()

	if cfg.MichelinHttpPort != 0 {
		//开始监听米其林小车组HTTP端口
		MqlHS := NewMichelinHTTPServer(cfg, dbo, connManager)
		MqlHS.Listen()
	}
	if cfg.CommandHttpPort != 0 {
		//开始监听指令下发Http端口
		CommandHS := NewCommandHTTPServer(cfg, dbo, connManager)
		CommandHS.Listen()
	}

	//开始监听TCP端口
	Listen(cfg, dbo, connManager)

	//监听信号
	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

	s := <-signalChan
	zLog(nil, VER, " 收到退出信号:", s)
}
